# -*- coding: utf-8 -*-
#
# Copyright 2017 zhangxp, st
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from rqalpha.interface import AbstractMod

from .data_source import St_Day_Min_KDataSource

g_data_source = None

class StuDayMinMod(AbstractMod):
    def __init__(self):
        pass

    def __del__(self):
        """"""
        global g_data_source
        if g_data_source is not None:
            g_data_source.release()
            # print('del g_data_source')
            g_data_source = None

    def start_up(self, env, mod_config):
        global g_data_source
        bundle_path = env.config.base.data_bundle_path
        if g_data_source is None:
            if 'instrument_types' in mod_config:
                g_data_source = St_Day_Min_KDataSource(bundle_path, instrument_types=mod_config.instrument_types)
            else:
                g_data_source = St_Day_Min_KDataSource(bundle_path)
            # print('new g_data_source')
        env.set_data_source(g_data_source)

    def tear_down(self, code, exception=None):
        global g_data_source
        if g_data_source is not None:
            g_data_source.release()
            # print('del g_data_source')
            g_data_source = None
        pass
